import requests, json
import time


class building:
    def __init__(self):
        self._url = "http://61.156.49.114:8000/reisPub/pub/loupan?buildingId="
        self._session = requests.session()
        # self._session.adapters.DEFAULT_RETRIES = 2
        # self._session.headers['timeout'] = '30'
        self.headers = {
            "Host": "61.156.49.114:8000",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
            "Accept": "image/avif,image/webp,*/*",
            "Connection": "close"
        }
    def get_code(self,id):
        code = ''
        try:
            response = self._session.get(self._url + "536173",  headers=self.headers,timeout=30, verify=False)
            response.close()
        except:
            print('获取超时')
            time.sleep(5)
            return 0
        print(response.text)
        # if response.status_code == 200:
        #     with open(picname + ".jpg", "wb") as f:
        #         f.write(response.content)
        # else:
        #     print("图片下载失败")

        '''
        //536174
        $title_regex4 = "/<h3 class=\"h3\">.*?<\/h3>/ism";
        if (preg_match_all($title_regex4, $string, $matches)) {
          $build = $matches[0][0];
          $buildname = strip_tags($build);
          preg_match('/-?[1-9]\d*/', $buildname, $buildno);
    
          $mapdata['buildid'] = $buildingId;
          $mapdata['buildno'] = $buildno[0];//楼号
          $mapdata['buildname'] = $buildname;
          $mapdata['html'] = $string;
          $mapdata['date'] = time();
          M('build_orign')->add($mapdata);
        '''
        return code

    def get_file_content(self, filePath):
        with open(filePath, "rb") as fp:
            return fp.read()

if __name__ == "__main__":
    can = building()
    code = can.get_code(1)
    # while True:
    #     code = can.get_code()
    #     res = can.check(code)
    #
    #     if res["status"] == 200:
    #         print(res)
    #         break

